package com.zhida.smartcontrol.dialog.kgsjdialog;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.LogUtil;
import com.jay.tools.utils.ResUtil;
import com.tan.linkpicker.pickerview.builder.TimePickerBuilder;
import com.tan.linkpicker.pickerview.listener.CustomListener;
import com.tan.linkpicker.pickerview.listener.OnTimeSelectChangeListener;
import com.tan.linkpicker.pickerview.view.TimePickerView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/3 003 15:39
 * description: 开关时间
 */
public class KgsjDialogFragment extends MVPBaseDialogFragment<KgsjDialogContract.View, KgsjDialogPresenter> implements KgsjDialogContract.View {
    @BindView(R.id.btn_auto)
    ButtonStyle mBtnAll;
    @BindViews({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7})
    List<ButtonStyle> mBtnList;
    @BindView(R.id.btn_week_all)
    ButtonStyle mBtnWeek;
    @BindViews({R.id.btn_week_1, R.id.btn_week_2, R.id.btn_week_4, R.id.btn_week_8, R.id.btn_week_16, R.id.btn_week_32, R.id.btn_week_64})
    List<ButtonStyle> mBtnWeekList;
    @BindView(R.id.ll_way)
    LinearLayout mWayView;
    @BindView(R.id.ll_way_1)
    LinearLayout mWayView1;
    @BindViews({R.id.fl_time_1, R.id.fl_time_1_2, R.id.fl_time_2, R.id.fl_time_2_2, R.id.fl_time_3, R.id.fl_time_3_2})
    List<FrameLayout> mFlList;

    private String[] mTimeArr = new String[]{"", "", "", "", "", ""};

    private List<LampBean> mLampList;

    public static KgsjDialogFragment newInstance(String switchType, List<LampBean> data) {
        Bundle args = new Bundle();
        args.putString("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        KgsjDialogFragment fragment = new KgsjDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_kgsj;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        mBtnAll.setText("全部");
        String switchType = getArguments().getString("type");
        mLampList = getArguments().getParcelableArrayList("list");

        mPresenter.showWayButton(switchType);

        for (int i = 0; i < mFlList.size(); i++) {
            initCustomTimePicker(mFlList.get(i), i);
        }
        for (int i = 0; i < 7; i++) {
            ButtonStyle button = mBtnWeekList.get(i);
            button.setId(i);
        }
    }

    @Override
    public void showButton(int size) {
        // 1,2,0,4
        if (size == 8) {
            mWayView.setVisibility(View.VISIBLE);
            mWayView1.setVisibility(View.VISIBLE);
        } else if (size == 4) {
            mWayView.setVisibility(View.VISIBLE);
            mWayView1.setVisibility(View.GONE);
        }else {
            mWayView.setVisibility(View.GONE);
            mWayView1.setVisibility(View.GONE);
        }

        for (int i = 0; i < 8; i++) {
            ButtonStyle button = mBtnList.get(i);
            button.setId(i);
            if (i < size) {
                button.setVisibility(View.VISIBLE);
            } else {
                button.setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm, R.id.btn_auto, R.id.btn_week_all})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_auto:
                mBtnAll.setChecked(!mBtnAll.isChecked());
                LogUtil.e(TAG, "选中：" + mBtnAll.isChecked());
                for (ButtonStyle button : mBtnList) {
                    button.setChecked(mBtnAll.isChecked());
                }
                break;
            case R.id.btn_week_all:
                mBtnWeek.setChecked(!mBtnWeek.isChecked());
                LogUtil.e(TAG, "选中：" + mBtnWeek.isChecked());
                for (ButtonStyle button : mBtnWeekList) {
                    button.setChecked(mBtnWeek.isChecked());
                }
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                mPresenter.requestSetTime(mLampList, mTimeArr, mBtnWeekList, mBtnList);
                break;
        }
    }

    @OnClick({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7})
    public void clickWay(View v) {
        int id = v.getId();
        mBtnAll.setChecked(false);
        boolean checked = mBtnList.get(id).isChecked();
        mBtnList.get(id).setChecked(!checked);
    }

    @OnClick({R.id.btn_week_1, R.id.btn_week_2, R.id.btn_week_4, R.id.btn_week_8, R.id.btn_week_16, R.id.btn_week_32, R.id.btn_week_64})
    public void clickWeek(View v) {
        int id = v.getId();
        mBtnWeek.setChecked(false);
        boolean checked = mBtnWeekList.get(id).isChecked();
        mBtnWeekList.get(id).setChecked(!checked);
    }

    private void initCustomTimePicker(FrameLayout frameLayout, final int id) {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(Calendar.HOUR_OF_DAY, 0);
        selectedDate.set(Calendar.MINUTE, 0);
        selectedDate.set(Calendar.SECOND, 0);
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(3019, 12, 31);

        // 设置默认时间
        mTimeArr[id] = getTime(selectedDate.getTime());

        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(mActivity, null)
                .setLayoutRes(R.layout.layout_time_picker, new CustomListener() {
                    @Override
                    public void customLayout(View v) {

                    }
                })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        String time = getTime(date);
                        mTimeArr[id] = time;
                        LogUtil.e(TAG, id + " ========== " + time);
                    }
                })
                .setItemsVisible(3)
                .setType(new boolean[]{false, false, false, true, true, false})
                .setLabel("", "", "", ":", "", "")
                .setContentTextSize(20)
                .setTextColorCenter(ResUtil.getColor(mActivity, R.color.c_3862FF))
                .setDate(selectedDate)
                .setRangDate(startDate, selectedDate)
                .setDividerColor(0x00FFFFFF)
                .setDecorView(frameLayout)
                .build();

        pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
        pvTime.show();
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    @Override
    public void onSuccess() {
        dismiss();
    }


}
